package kr.co.dongpang.android;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.text.DecimalFormat;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.dongpang.domain.ArrayNotice;
import com.google.gson.Gson;
import com.sys.app.cms.IdPassword;

import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ListView;
import android.widget.AbsListView.OnScrollListener;

@SuppressLint("NewApi")
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class DmNoticeFragment extends Fragment {

	DecimalFormat df = new DecimalFormat("###,###");
	String vcode;
	
	static class ViewHolder {
		TextView regdate;
		TextView point;
		TextView totpoint;
		TextView getoruseC;
		int position;
	}

	public DmNoticeFragment(){
		
	}
	
	ArrayList<ArrayNotice> datas = new ArrayList<ArrayNotice>();

	ListView list;
    MyAdapter adapter;
    
    private static int listPage;
    protected boolean isExistLast;
    
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
 
        View rootView = inflater.inflate(R.layout.dm_order, container, false);
        ((ActionBarActivity)getActivity()).getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
		list = (ListView)rootView.findViewById(R.id.list);
		
		listPage = 0;
		
		
		// Getting adapter by passing xml data ArrayList
		adapter = new MyAdapter(getActivity(), R.layout.com_notice_row, datas);
		
//		LayoutInflater flater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//		LinearLayout header = (LinearLayout) flater.inflate(R.layout.dm_order_header, null);
//		list.addHeaderView(header);
		
        list.setAdapter(adapter);    
		list.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
				final int lastItem = firstVisibleItem + visibleItemCount;
				if (lastItem == totalItemCount) {
					if (isExistLast == false) {
						isExistLast = true;
						Log.d("Last", "Last");
						new JsonReadTask().execute("" + listPage++, null, null);
						Log.d("Last", "Last:"+listPage);
					}
				} else {
					if (isExistLast == true) {
						isExistLast = false;
					}
				}
			}
		});

		
		if (datas == null || datas.size() == 0) {
			new JsonReadTask().execute(""+listPage++, null, null); 
		}        
        
		
        return rootView;
    }
	

	
	class JsonReadTask extends AsyncTask<String, Void, Void> {

		String page;
		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected Void doInBackground(String... params) {
			
			page = params[0];

			String jUrl = "http://dongpang.co.kr/android/dongpang.v01/dm_notice.php?&page=" + page;
			String str = JSONfunctions.getStringfromURL(jUrl);
			Gson gson = new Gson();
			ArrayNotice[] datas2 = gson.fromJson(str, ArrayNotice[].class);
			for (ArrayNotice data : datas2) {
				datas.add(data);
			}	
			
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			Log.d("dongpang", "onPostExecute");
			adapter.notifyDataSetChanged();
			
			
			super.onPostExecute(result);
		}

	}		
	
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
	}

	class MyAdapter extends ArrayAdapter<ArrayNotice> {
		
		protected static final int REQUEST_DETAIL = 0;

		public MyAdapter(Context context, int resource, ArrayList<ArrayNotice> items) {
			super(context, resource, items);
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {

			if (convertView == null) {
				convertView = getActivity().getLayoutInflater().inflate(R.layout.com_notice_row, null);
			}
			
			
	    	((TextView)convertView.findViewById(R.id.title)).setText(getItem(position).title);
	    	((TextView)convertView.findViewById(R.id.regdt)).setText(getItem(position).regdt);
	    	
			convertView.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					
					Intent intent = new Intent(getActivity(), DmNoticeView.class);
					intent.putExtra("boardseq", getItem(position).boardseq);
					startActivity(intent);

				}
				
			});
			
			return convertView;
		}

	}	
	
	
	
	
	
	
}
